from hep.aida import *

from java.awt import Color
from java.awt import Font
from java.lang import *
from java.util import *
from java.io import *

from jhplot.jadraw import *
from jhplot  import *

from fr.in2p3.ipnl.aidautilities import *
from fr.in2p3.ipnl.oflap.scint import *
from jscinttools.utilities.jhplotutilities import *

Locale.setDefault(Locale.ENGLISH)
System.getProperties().setProperty("hep.aida.BatchAnalysisFactory","hep.aida.ref.BatchAnalysisFactory")

showFigures = True
writeFigures = not showFigures

""" Define colors and fonts """
figType = [".png", ".eps"]
colors = [Color.blue, Color.red, Color(0, 170, 0), Color.magenta, Color.black, Color.yellow]
axisLabelFont = Font(Font.SERIF, Font.PLAIN, 25)
titleFont = Font(Font.SERIF, Font.PLAIN, 25)
legendFont = Font(Font.SERIF, Font.PLAIN, 20)
ticFont = Font(Font.SERIF, Font.PLAIN, 20)
statBoxFont = Font(Font.SERIF, Font.PLAIN, 15)

""" Define the Path and files names """
aidaPath = "cygdrive/d/jCode/My/BGO_Sources"
aidaFile = "All_Sources"
outPath = "/home/marc-antoine/Dropbox/BGO"
afr = AidaFileReader(aidaPath, aidaFile)

"""
###############################################################
# Plotting Part
###############################################################
"""
""" Create the plottable JHep objects from AIDA IObjects """
""" The DataPointSet """
lyVsEnEff_Norm = []
lyVsEnEffKey_Norm = []

linNormPlotter = HPlotJa("LY Linearity Norm", 600, 600, 1, 1, True)
linNormPlotter.setGTitle("Light Yield Linearity in BGO", titleFont)
linNormPlotter.antiAlias = False
if showFigures:
	linNormPlotter.visible(True);
else:
	linNormPlotter.visible(False);

linNormPlotter.cd(1, 1)
linNormPlotter.setPad(1, 1, 0.125, 0.125, 0.75, 0.75)
linNormPlotter.setRange(0, 700, 0.5, 1.2)
linNormPlotter.ticFont = ticFont
linNormPlotter.showStatBox = False
linNormPlotter.showKey(False)
linNormPlotter.nameX = "Energy (keV)"
linNormPlotter.nameY = "Normalized LY"

theAxes = linNormPlotter.getPad()
theAxes.setLabelSpace(0, 0.0)
theAxes.setLabelSpace(1, 0.0)

labX = linNormPlotter.getLabelX()[0][0]
labX.font = axisLabelFont
HPlotJaUtil.setLabelLocationAtCenter(0, labX, linNormPlotter)
labY = linNormPlotter.getLabelY()[0][0]
labY.font = axisLabelFont
HPlotJaUtil.setLabelLocationAtCenter(1, labY, linNormPlotter)

#symbols = [0, 3, 10, 2]
symbols = [2, 9, 3]
temperature = ["3", "10", "30"]

for i in range(len(temperature)):
	lyVsEnEff_Norm.append(P1D(afr.getDPS("LY Lin Eff T=" + temperature[i] + ".0K Norm")))
	lyVsEnEff_Norm[i].title = "T = " + temperature[i] + " K"
	lyVsEnEff_Norm[i].style = "p"
	lyVsEnEff_Norm[i].symbol = symbols[i]
	lyVsEnEff_Norm[i].symbolSize = 16
	lyVsEnEff_Norm[i].color = colors[i]
	lyVsEnEff_Norm[i].errY = True
	lyVsEnEff_Norm[i].penWidthErr = 1
	lyVsEnEff_Norm[i].errX = False
	lyVsEnEff_Norm[i].setErrColor(lyVsEnEff_Norm[i].getColor())
	linNormPlotter.draw(lyVsEnEff_Norm[i])

	lyVsEnEffKey_Norm.append(JaKey(lyVsEnEff_Norm[i].symbol, lyVsEnEff_Norm[i].title, 0.60, 0.70 + (-i * 0.05), "NDC"))
	lyVsEnEffKey_Norm[i].color = lyVsEnEff_Norm[i].color
	lyVsEnEffKey_Norm[i].keyColor = lyVsEnEff_Norm[i].color
	lyVsEnEffKey_Norm[i].font = legendFont
	lyVsEnEffKey_Norm[i].penWidth = 1
	linNormPlotter.add(lyVsEnEffKey_Norm[i])

linNormPlotter.update()

""" Saves or not the plot """
if writeFigures:
	for i in range(len(figType)):
		outFileName = "linNorm_test" + figType[i]
		linNormPlotter.export(outPath + File.separator + outFileName)
		print("Writting File : " + outPath + File.separator + outFileName)
		
linNormPlotter.update()
print("That's all folks !")

